<?php

/**
 * Contains "Mvc" mvc class
 *
 * LICENSE: MIT
 *
 * @author Peter Pippinger
 * @category PP
 * @package PP_Mvc
 * @copyright Copyright (c) 2013 Peter Pippinger
 * @license http://opensource.org/licenses/MIT
 * @version 1.1.0
 * @since Class exists since release 1.0.0
 */

/**
 * "Mvc" mvc class
 *
 * Provides model view controller handling for PHP
 *
 * @author Peter Pippinger
 * @category PP
 * @package PP_Mvc
 * @copyright Copyright (c) 2013 Peter Pippinger
 * @license http://opensource.org/licenses/MIT
 * @version 1.1.0
 * @since Class exists since release 1.0.0
 */
class Mvc
{
	/**
	 * Model object.
	 *
	 * @var object
	 */
	public $model;

	/**
	 * Controller object.
	 *
	 * @var object
	 */
	public $controller;

	/**
	 * View object.
	 *
	 * @var object
	 */
	public $view;

	/**
	 * Mvc objects for singleton modules.
	 *
	 * @var array
	 */
	private static $objectsMvc;

	/**
	 * Initializes Mvc class.
	 * Creates the code outline for the given modul, if it's not exists.
	 *
	 * @param string $modul        	
	 */
	private function __construct($modul)
	{
		$modelClassName = "Model$modul";
		$viewClassName = "View$modul";
		$controllerClassName = "Controller$modul";
		
		if (! is_dir(ROOT_PATH . "/Lib/Mvc/Modules/$modul")) {
			mkdir(ROOT_PATH . "/Lib/Mvc/Modules/$modul");
		}
		
		if (! is_dir(ROOT_PATH . "/Lib/Mvc/Modules/$modul/phtml")) {
			mkdir(ROOT_PATH . "/Lib/Mvc/Modules/$modul/phtml");
		}
		
		foreach (array(
			"Model",
			"View",
			"Controller"
		) as $mvc) {
			if (! is_file(ROOT_PATH . "/Lib/Mvc/Modules/$modul/$mvc$modul.php")) {
				$contents = file_get_contents(ROOT_PATH . "/Lib/Mvc/Modules/MvcTemplate/{$mvc}MvcTemplate.php");
				$contents = str_replace("MvcTemplate", $modul, $contents);
				file_put_contents(ROOT_PATH . "/Lib/Mvc/Modules/$modul/$mvc$modul.php", $contents);
			}
		}
		
		if (! is_file(ROOT_PATH . "/Lib/Mvc/Modules/$modul/phtml/$modul.phtml")) {
			$contents = file_get_contents(ROOT_PATH . "/Lib/Mvc/Modules/MvcTemplate/phtml/Template.phtml");
			$contents = str_replace("MvcTemplate", $modul, $contents);
			file_put_contents(ROOT_PATH . "/Lib/Mvc/Modules/$modul/phtml/$modul.phtml", $contents);
		}
		
		require_once ROOT_PATH . "/Lib/Mvc/Modules/$modul/$modelClassName.php";
		require_once ROOT_PATH . "/Lib/Mvc/Modules/$modul/$viewClassName.php";
		require_once ROOT_PATH . "/Lib/Mvc/Modules/$modul/$controllerClassName.php";
		
		$this->model = new $modelClassName();
		$this->controller = new $controllerClassName($this->model);
		$this->view = new $viewClassName($this->controller, $this->model);
	}

	/**
	 * Returns the output of this mvc object.
	 *
	 * @return string
	 */
	public function output()
	{
		$this->model->outputArguments = func_get_args();
		$output = $this->view->output();
		return $output;
	}

	/**
	 * Singleton factory for the given mvc modul.
	 *
	 * @param string $modul        	
	 * @return object
	 */
	public static function modul($modul)
	{
		if (! isset(Mvc::$objectsMvc[$modul]))
			Mvc::$objectsMvc[$modul] = new Mvc($modul);
		return Mvc::$objectsMvc[$modul];
	}
}
